//
//  AddressBookViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/26.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "AddressBookViewController.h"
#import "AddressBookTableViewCell.h"
#import "PersonModel.h"

@interface AddressBookViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>{
    UIImageView *headImage;
    UITableView *table;
    NSArray *inforArray;
    NSArray *labelArray;
    PersonModel *models;
    
}

@end

@implementation AddressBookViewController
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    models = [PersonModel yy_modelWithDictionary:self.dic];
    NSLog(@"%@",self.dic);
    //判断是那个类型，如果是个人就用个人资料卡片，公司用公司卡片
    if ([self.type isEqualToString:@"person"]) {
        [self detailWithDic];
        [self setUIPerson];
    }else if([self.type isEqualToString:@"company"]){
        [self setUIDepartment];
    }
    
    
}
- (void)detailWithDic{
    inforArray = [NSArray new];
    inforArray = @[models.DepName,models.Phone,models.QQ,models.Tel,models.UserName,models.sexName,models.Email,models.HomeAddress];
    labelArray = @[@"部门:",@"手机:",@"QQ:",@"电话:",@"用户名:",@"性别:",@"电子邮件:",@"主页:"];
}

- (void)setUIPerson{
    
    WS(ws);
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor =  [UIColor colorWithRed:0.396 green:0.365 blue:0.357 alpha:1.000];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    scrollView.bounces = NO;
    scrollView.scrollEnabled = YES;
    scrollView.scrollsToTop = NO;
    
    UIView *container = [UIView new];
    [scrollView addSubview:container];
    container.userInteractionEnabled = YES;
    container.backgroundColor = [UIColor colorWithRed:0.396 green:0.365 blue:0.357 alpha:0.8];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scrollView);
        make.edges.equalTo(scrollView).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    
    UIView *ImageUnder = [[UIView alloc]init];
    [container addSubview:ImageUnder];
    ImageUnder.backgroundColor = [UIColor colorWithRed:0.753 green:0.741 blue:0.737 alpha:1.000];
    [ImageUnder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(120*FitSize,120*FitSize));
    }];
    ImageUnder.layer.cornerRadius = 60*FitSize;
    
    headImage = [[UIImageView alloc]init];
    if ([models.sexName isEqualToString:@"男"]) {
        
        headImage.image = [UIImage imageNamed:@"通讯录详情"];
        
    }else{
        
        headImage.image = [UIImage imageNamed:@"通讯录详情女"];
        
    }
    [ImageUnder addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(ImageUnder);
        make.size.mas_equalTo(CGSizeMake(114*FitSize, 114*FitSize));
    }];
    headImage.layer.cornerRadius = 57*FitSize;
    headImage.layer.masksToBounds = YES;
    
    UILabel *name = [[UILabel alloc]init];
    name.text = models.RealName;
    name.textAlignment = NSTextAlignmentCenter;
    name.textColor = [UIColor whiteColor];
    name.font = [UIFont systemFontOfSize:23];
    [container addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ImageUnder.mas_bottom).offset(10*FitSize);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(KSWidth);
        make.height.mas_equalTo(25);
    }];
    
    UILabel *companyName = [[UILabel alloc]init];
    companyName.text = models.cName;
    companyName.textAlignment = NSTextAlignmentCenter;
    companyName.textColor = [UIColor whiteColor];
    companyName.font = [UIFont systemFontOfSize:15];
    [container addSubview:companyName];
    [companyName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).offset(5*FitSize);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(KSWidth);
        make.height.mas_equalTo(40);
    }];
    table = [[UITableView alloc]init];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(companyName.mas_bottom).offset(30);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(15, 30, 40, 40);
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

- (void)setUIDepartment{
    WS(ws);
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.userInteractionEnabled = YES;
    scrollView.backgroundColor =  [UIColor colorWithRed:0.396 green:0.365 blue:0.357 alpha:1.000];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    scrollView.bounces = NO;
    scrollView.scrollEnabled = YES;
    scrollView.scrollsToTop = NO;
    
    UIView *container = [UIView new];
    container.userInteractionEnabled = YES;
    [scrollView addSubview:container];
    container.backgroundColor = [UIColor colorWithRed:0.396 green:0.365 blue:0.357 alpha:0.8];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scrollView);
        make.edges.equalTo(scrollView).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    
    UIView *ImageUnder = [[UIView alloc]init];
    [container addSubview:ImageUnder];
    ImageUnder.backgroundColor = [UIColor colorWithRed:0.753 green:0.741 blue:0.737 alpha:1.000];
    [ImageUnder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(120*FitSize,120*FitSize));
    }];
    ImageUnder.layer.cornerRadius = 60*FitSize;
    
    headImage = [[UIImageView alloc]init];
    headImage.image = [UIImage imageNamed:@"head"];
    [ImageUnder addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(ImageUnder);
        make.size.mas_equalTo(CGSizeMake(114*FitSize, 114*FitSize));
    }];
    headImage.layer.cornerRadius = 57*FitSize;
    headImage.layer.masksToBounds = YES;
    
    UILabel *name = [[UILabel alloc]init];
    if (models.DepName.length > 0) {
        name.text = [NSString stringWithFormat:@"负责人:  %@",models.DepManager];
    }else{
        name.text = @"负责人:  ❌";
    }
    
    name.textAlignment = NSTextAlignmentCenter;
    name.textColor = [UIColor whiteColor];
    name.font = [UIFont systemFontOfSize:25];
    [container addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ImageUnder.mas_bottom).offset(20*FitSize);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(KSWidth);
        make.height.mas_equalTo(25);
    }];
    
    CGFloat font = 20;
    CGFloat margin = 15;
    
    //公司
    UILabel *companyName = [[UILabel alloc]init];
    if (models.cName.length > 0) {
        companyName.text = [NSString stringWithFormat:@"公司名称:  %@",models.cName];
    }else{
        companyName.text = @"公司名称:  ❌";
    }
    
    companyName.textAlignment = NSTextAlignmentCenter;
    companyName.textColor = [UIColor whiteColor];
    companyName.font = [UIFont systemFontOfSize:font];
    [container addSubview:companyName];
    [companyName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).offset(margin*FitSize);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(KSWidth);
        make.height.mas_equalTo(40);
    }];
    
    //部门
    UILabel *department = [[UILabel alloc]init];
    if (models.DepName.length > 0) {
        department.text = [NSString stringWithFormat:@"部门:  %@",models.DepName];
    }else{
        department.text = @"部门:  ❌";
    }
    
    department.textAlignment = NSTextAlignmentCenter;
    department.textColor = [UIColor whiteColor];
    department.font = [UIFont systemFontOfSize:font];
    [container addSubview:department];
    [department mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(companyName.mas_bottom).offset(margin*FitSize);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(KSWidth);
        make.height.mas_equalTo(40);
    }];
    //手机号码
    UIButton *Phone = [UIButton buttonWithType:UIButtonTypeCustom];
    
    if (models.Phone.length > 0) {
        [Phone setTitle:[NSString stringWithFormat:@"手机:  %@",models.Phone] forState:UIControlStateNormal];
    }else{
        [Phone setTitle:@"手机:  ❌" forState:UIControlStateNormal];
    }
    
    
    [Phone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    Phone.titleLabel.font = [UIFont systemFontOfSize:font];
    [self.view addSubview:Phone];
    [Phone addTarget:self action:@selector(callCompanyPhone) forControlEvents:UIControlEventTouchUpInside];
    [Phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(department.mas_bottom).offset(margin*FitSize);
        make.centerX.equalTo(container);
        make.width.mas_equalTo(250*FitSize);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneButton addTarget:self action:@selector(callCompanyPhone) forControlEvents:UIControlEventTouchUpInside];
    [phoneButton setImage:[UIImage imageNamed:@"手机"] forState:UIControlStateNormal];
    [self.view addSubview:phoneButton];
    [phoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(department.mas_bottom).offset(margin*FitSize);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(15, 30, 40, 40);
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
}

- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*FitSize;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return inforArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellID = @"CellID";
    AddressBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[AddressBookTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        [cell configWithInfoArray:inforArray labelArrat:labelArray index:indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:{
            [self callPhone:indexPath.row];
        }
            break;
        case 3:{
            [self callPhone:indexPath.row];
        }
            break;
            
        default:
            break;
    }
}

- (void)callPhone:(NSInteger )row{
    NSString *phone = @"";
    switch (row) {
        case 1:{
            phone = models.Phone;
        }
            break;
        case 3:{
            phone = models.Tel;
        }
            break;
        default:{
            
        }
            break;
    }
    if (phone.length > 0) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
    }else{
        [XHToast showBottomWithText:@"电话号码为空"];
    }
}
- (void)callCompanyPhone{
    if (models.Phone.length > 0) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",models.Phone];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
    }else{
        [XHToast showBottomWithText:@"电话号码为空"];
    }
    
    
}
@end
